package com.jhws.core.push;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.jhws.common.PropertiesReader;
import com.jhws.common.base.bo.Global;
import com.jhws.util.LogHelper;
import com.jhws.util.StringUtils;
import com.tencent.xinge.*;
import org.json.JSONObject;

public class PushXinGe {

    private static final Long appId = 2100160540L;
    private static final String appSecret = "2aa01a28a4108f5ad05fa35c352ad0c5";
    private static final Long iosAppId = 2200179283L;
    private static final String iosAppSecret = "af60ea28cb8b243a0bc592555b4c33fe";
    /*用来存储信鸽的第三方IOS的appID,呼叫媒体服务器使用 */
    public static final String REDIS_XINGE_APPID = "xinge:comm";

    // android下发交集标签设备（同时满足两个标签）
    public static JSONObject androidMultiplePushTags(String logic, String content, String titile, List<String> tagList) {
        XingeApp xinge = new XingeApp(appId, appSecret);
        Message message = new Message();
        message.setMultiPkg(1);
        message.setTitle(titile);
        message.setContent(content);
        message.setType(Message.TYPE_MESSAGE);
        message.setExpireTime(86400);
        JSONObject ret = xinge.pushTags(0, getXgMadeTagList(tagList), logic, message);
        return (ret);
    }

    public static JSONObject androidPushTagWithCustom(String content, String titile, String tag, Map map) {
        XingeApp xinge = new XingeApp(appId, appSecret);
        Message message = new Message();
        message.setMultiPkg(1);
        message.setTitle(titile);
        message.setContent(content);
        message.setType(Message.TYPE_MESSAGE);
        message.setExpireTime(86400);
        message.setCustom(map);
        List<String> list = new ArrayList<>();
        list.add(tag);
        JSONObject ret = xinge.pushTags(0, getXgMadeTagList(list), "OR", message);
        return (ret);
    }

    // ios下发交集标签设备（同时满足两个标签）
    public static JSONObject iosMultiplePushTags(String logic, MessageIOS message, List<String> tagList) {
        XingeApp xinge = new XingeApp(iosAppId, iosAppSecret);
        JSONObject ret = xinge.pushTags(0, tagList, logic, message, XingeApp.IOSENV_PROD);

        xinge.pushTags(0, getXgMadeTagList(tagList), logic, message, XingeApp.IOSENV_DEV);
        return (ret);
    }


    // 下发交并集签设备（有一个标签都能收到相应的信息）
    public static JSONObject demoPushTags() {
        XingeApp xinge = new XingeApp(2100160540, "2aa01a28a4108f5ad05fa35c352ad0c5");
        List<String> tagList = new ArrayList<String>();
        tagList.add("outdoor");
        Message message = new Message();
        message.setMultiPkg(1);
        message.setTitle("title 并集");
        message.setContent("content 并集");
        message.setType(Message.TYPE_MESSAGE);
        message.setExpireTime(86400);
        JSONObject ret = xinge.pushTags(0, tagList, "OR", message);
        return (ret);
    }

    // 下发单个账号（别名 000108010001）
    public static JSONObject androidPushSingleAccount(String account, String titile, String content) {
        XingeApp xinge = new XingeApp(appId, appSecret);
        Message message = new Message();
        message.setMultiPkg(1);
        message.setTitle(titile);
        message.setContent(content);
        message.setType(Message.TYPE_MESSAGE);
        message.setExpireTime(86400);
        JSONObject ret = xinge.pushSingleAccount(0, getXgMade(account), message);
        System.out.println("------------------> android " + getXgMade(account) + "   ret:" + ret);
        return (ret);
    }

    // 下发单个账号（别名 000108010001）
    public static JSONObject androidPushSingleAccountWithCustom(String account, String titile, String content, Map map) {
        XingeApp xinge = new XingeApp(appId, appSecret);
        Message message = new Message();
        message.setMultiPkg(1);
        message.setTitle(titile);
        message.setContent(content);
        message.setType(Message.TYPE_MESSAGE);
        message.setExpireTime(86400);
        message.setCustom(map);
        JSONObject ret = xinge.pushSingleAccount(0, getXgMade(account), message);
        System.out.println("------------------> android " + getXgMade(account) + "   ret:" + ret);
        return (ret);
    }

    // 下发单个账号（别名 000108010001）
    public static JSONObject iosPushSingleAccount(String account, String key, String alert, String content) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put(key, content);
        return (iosPushSingleAccountWithCustom(account, alert, map));
    }

    public static JSONObject iosPushSingleAccountWithCustom(String account, Object alert, Map map) {

        MessageIOS messageIOS = new MessageIOS();
        messageIOS.setCustom(map);
        if (alert instanceof String)
            messageIOS.setAlert((String) alert);
        else
            messageIOS.setAlert((JSONObject) alert);
        messageIOS.setExpireTime(86400);
        messageIOS.setSound("default");
        XingeApp xinge = new XingeApp(iosAppId, iosAppSecret);
        JSONObject ret = xinge.pushSingleAccount(0, getXgMade(account), messageIOS, XingeApp.IOSENV_PROD);
        xinge.pushSingleAccount(0, getXgMade(account), messageIOS, XingeApp.IOSENV_DEV);
        System.out.println("------------------> ios " + getXgMade(account) + "   ret:" + ret);
        return (ret);
    }

    public static JSONObject iosPushTagWithCustom(Object alert, String tag, Map map) {
        MessageIOS messageIOS = new MessageIOS();
        messageIOS.setCustom(map);
        if (alert instanceof String)
            messageIOS.setAlert((String) alert);
        else
            messageIOS.setAlert((JSONObject) alert);
        messageIOS.setExpireTime(86400);
        messageIOS.setSound("default");
        XingeApp xinge = new XingeApp(iosAppId, iosAppSecret);

        List list = new ArrayList<String>();
        list.add(tag);
        JSONObject ret = xinge.pushTags(0, getXgMadeTagList(list), "OR", messageIOS, XingeApp.IOSENV_PROD);
        xinge.pushTags(0, getXgMadeTagList(list), "OR", messageIOS, XingeApp.IOSENV_DEV);
        System.out.println("------------------> ios " + tag + "   ret:" + ret);
        return (ret);
    }

    private static String getXgMade(String s) {
        String basePlatform = PropertiesReader.getProperty("basePlatform");
        if (!StringUtils.isEmpty(basePlatform)) {
            s = basePlatform + "-" + s;
        }
        return s;
    }

    private static List<String> getXgMadeTagList(List<String> tagList) {
        List<String> retTagList = new ArrayList<>();
        for (String s : tagList) {
            retTagList.add(getXgMade(s));
        }
        return retTagList;
    }
}
